import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  }, {
    path: '/home',
    name: 'home',
    redirect: '/home/about',
    component: () => import('../views/HomeView.vue'),
    children: [{
      path: 'about',
      name: 'about',
      component: () => import('../views/AboutView.vue')
    }, {
      path: 'userinfo',
      name: 'userinfo',
      component: () => import('../views/userinfo/UserinfoList.vue')
    }, {
      path: 'role',
      name: 'role',
      component: () => import('../views/role/RoleList.vue')
    }, {
      path: 'department',
      name: 'department',
      component: () => import('../views/department/DepartmentList.vue')
    }, {
      path: 'brand',
      name: 'brand',
      component: () => import('../views/brand/BrandList.vue')
    }, {
      path: 'car',
      name: 'car',
      component: () => import('../views/car/CarList.vue')
    }, {
      path: 'weixiu',
      name: 'weixiu',
      meta: { type: 2 },
      component: () => import('../views/record/RecordList.vue')
    }, {
      path: 'baoyang',
      name: 'baoyang',
      meta: { type: 1 },
      component: () => import('../views/record/RecordList.vue')
    }, {
      path: 'apply',
      name: 'apply',
      component: () => import('../views/apply/ApplyUpdate.vue')
    }, {
      path: 'myapply',
      name: 'myapply',
      meta: { type: 1 },
      component: () => import('../views/apply/ApplyList.vue')
    }, {
      path: 'allapply',
      name: 'allapply',
      meta: { type: 2 },
      component: () => import('../views/apply/ApplyList.vue')
    }, {
      path: 'exam',
      name: 'exam',
      meta: { type: 3 },
      component: () => import('../views/apply/ApplyList.vue')
    }, {
      path: 'agree',
      name: 'agree',
      meta: { type: 4 },
      component: () => import('../views/apply/ApplyList.vue')
    }]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/') { // 放行名单
    next()
  } else {
    if (Vue.$cookies.get('token')) {
      next()
    } else {
      Vue.prototype.$alert('请先登录！', { type: 'warning' }).then(() => {
        next('/')
      })
    }
  }
})

export default router
